const fs = require("fs");
let data = require("./项目.json");

let head = `
| 名字           | 意义 | 类型 | 默认值 |
| -------------- | ---- | ---- | ------ |`;
let count = 0;
let arr = [];
for (var k in data) {
  let item = {
    key: k,
    value: data[k],
  };
  console.log("k ---------开始----------");
  console.log("item =");
  console.log(item);
  let body = getBody(item);
  console.log(body);
  console.log("k ---------结束----------");
  arr = arr.concat(body);
  count++;
  // if (count > 1) {
  //   break;
  // }
}
console.log(arr);
arr.sort(function (a, b) {
  if (a.nameValue < b.nameValue) {
    return -1;
  } else {
    return 1;
  }
});
arr.map((item) => {
  console.log(JSON.stringify(item));
});

arr.map((item) => {
  console.log(getRecord(item));
});


// getRecord(item)

// function getRecord(item) {
//   let body = `| ${item.nameValue} | ${item.meaningValue} | ${item.typeValue} | ${item.defaultValue} |`;
//   return body;
// }



function getBody(item, lasResult, superName) {
  lasResult = lasResult || [];
  superName = superName || "";
  let itemType = toRawType(item.value);
  let key = item.key;
  let result = {};
  switch (itemType) {
    case "Array":
      result.nameValue = superName ? superName + "." + key : key;
      result.meaningValue = "";
      result.typeValue = "Array";
      result.defaultValue = 提取指定变量的信息(key).defaultValue;
      break;
    case "Object":
      let arr = [];
      for (var k in item.value) {
        arr = getBody(
          {
            key: k,
            value: item.value[k],
          },
          lasResult,
          superName
        );
        lasResult = lasResult.concat(arr);
      }
      return lasResult;
    case "Number":
      result.nameValue = superName ? superName + "." + key : key;
      result.meaningValue = "";
      result.typeValue = "number";
      result.defaultValue = 提取指定变量的信息(key).defaultValue;
      break;
    case "String":
      result.nameValue = superName ? superName + "." + key : key;
      result.meaningValue = "";
      result.typeValue = "string";
      result.defaultValue = 提取指定变量的信息(key).defaultValue;
      break;
    case "Boolean":
      result.nameValue = superName ? superName + "." + key : key;
      result.meaningValue = "";
      result.typeValue = "boolean";
      result.defaultValue = 提取指定变量的信息(key).defaultValue;
      break;
    default:
      console.log(item);
      console.log(itemType);
      throw new Error("其他类型");
  }
  lasResult = result;
  return [result];
}
function getRecord(item) {
  let body = `| ${item.nameValue} | ${item.meaningValue} | ${item.typeValue} | ${item.defaultValue} |`;
  return body;
}
function toRawType(value) {
  return Object.prototype.toString.call(value).slice(8, -1);
}

function 提取指定变量的信息(variableName) {
  console.log("提取指定变量的信息");
  console.log("variableName = " + variableName);
  let data = fs.readFileSync("./名字和默认值.txt", "utf8");

  let reg = new RegExp(
    '@SerializedName\\("' + variableName + '"\\) [\\s\\S]+? var (?:[\\w]+?): (?:[\\w<>?]+?) = ([\\w()"]+?)(?=(,|$))'
  );
  let paragraph = data.match(reg);
  if (paragraph && paragraph[0]) {
    return {
      defaultValue: paragraph[1],
    };
  } else {
    return {
      defaultValue: "",
    };
  }
}

// toRawType({}) //  Object
// toRawType([])  // Array
// toRawType(true) // Boolean
// toRawType(undefined) // Undefined
// toRawType(null) // Null
// toRawType(function(){}) // Function

// Object.prototype.toString.call('') ;   // [object String]
// Object.prototype.toString.call(1) ;    // [object Number]
// Object.prototype.toString.call(true) ; // [object Boolean]
// Object.prototype.toString.call(undefined) ; // [object Undefined]
// Object.prototype.toString.call(null) ; // [object Null]
// Object.prototype.toString.call({}) ; // [object Object]
// Object.prototype.toString.call([]) ; // [object Array]
// Object.prototype.toString.call(new Function()) ; // [object Function]
// Object.prototype.toString.call(new Date()) ; // [object Date]
// Object.prototype.toString.call(new RegExp()) ; // [object RegExp]
// Object.prototype.toString.call(new Error()) ; // [object Error]
